# -*- coding: utf-8 -*-
import scrapy
import datetime

class OtspiderSpider(scrapy.Spider):
    name = 'otspider'
    allowed_domains = ['otcbtc.io']

    def start_requests(self):
        wds = [
            'btc',
           
        ]
        start_url = 'https://otcbtc.io/sell_offers?currency='
        end_url = '&fiat_currency=cny&payment_type=all'

        scan_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        print('time %s :' % scan_time)

        for wd in wds:
            url = start_url + wd + end_url
            yield scrapy.Request(url=url, callback=self.parse)  # 爬取到的页面如何处理？提交给parse方法处理

    def parse(self, response):
        contents = {}

        res_unit = response.xpath(
            '//div[@class="recommend-card"]/div[@class="recommend-card__right-block"]/div[@class="recommend-card__unit"]/text()')
        otc = res_unit[0].extract().strip().replace('CNY/', '')

        # lowest price
        res_texts = response.xpath(
            '//div[@class="recommend-card"]/div[@class="recommend-card__right-block"]/div[@class="recommend-card__price"]/text()')
        lowest_price = res_texts[0].extract().strip()

        contents['title'] = otc
        contents['price'] = lowest_price
        print('%s : %s' % (otc, lowest_price))
        yield contents

        # print(contents)
        # print('======= lowest price start =======')
        # print('%s : %s' % (otc,lowest_price))
        # print('======= end =======')
